//
//  UIColor+Extension.m
//  iOSDevelopSDK
//
//  Created by Hayder on 2019/6/13.
//  Copyright © 2019 Hayder. All rights reserved.
//

#define HHRGBA(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]

#import "UIColor+HHExtension.h"

@implementation UIColor (HHExtension)

+ (UIColor *) colorWithHexString: (NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    // 判断前缀
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    // 从六位数值中找到RGB对应的位数并转换
    NSRange range;
    range.location = 0;
    range.length = 2;
    //R、G、B
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

+ (UIColor *)colorWithID:(NSString *)iddd
{
    if (!iddd || !iddd.length) {
        iddd = nil;
    }
    
    
    NSInteger str = [[iddd substringFromIndex:iddd.length - 1] integerValue];
    UIColor *color ;
    switch (str) {
        case 0:
            color = HHRGBA(72, 220, 108,1);
            break;
        case 1:
            color = HHRGBA(107, 114, 231,1);
            break;
        case 2:
            color = HHRGBA(1, 146, 255,1);
            break;
        case 3:
            color = HHRGBA(45, 186, 231,1);
            break;
        case 4:
            color = HHRGBA(97, 210, 254,1);
            break;
        case 5:
            color = HHRGBA(254, 74, 101,1);
            break;
        case 6:
            color = HHRGBA(255, 85, 52,1);
            break;
        case 7:
            color = HHRGBA(255, 165, 0,1);
            break;
        case 8:
            color = HHRGBA(254, 210, 1,1);
            break;
        case 9:
            color = HHRGBA(187, 89, 255,1);
            break;
        default:
            color = [UIColor whiteColor];
            break;
    }
    return color;
}
@end
